unit teknikdurum;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ppVar, ppBands, ppCtrls, ppPrnabl, ppClass, ppCache, ppProd, ppReport,
  ppDB, ppComm, ppRelatv, ppDBPipe, Db, ADODB, Grids, Wwdbigrd, Wwdbgrid,
  StdCtrls, wwdbdatetimepicker, Wwdotdot, Wwdbcomb, wwdblook, Mask,
  wwdbedit, ExtCtrls, Buttons, ImgList, TXComp, TXRB;

type
  TFtdurum = class(TForm)
    ImageList1: TImageList;
    ppDBPipeline1: TppDBPipeline;
    ppDBPipeline1ppField1: TppField;
    ppDBPipeline1ppField2: TppField;
    ppDBPipeline1ppField3: TppField;
    ppDBPipeline1ppField4: TppField;
    ppDBPipeline1ppField5: TppField;
    ppDBPipeline1ppField6: TppField;
    ppDBPipeline1ppField7: TppField;
    ppDBPipeline1ppField8: TppField;
    ppDBPipeline1ppField9: TppField;
    ppDBPipeline1ppField10: TppField;
    ppDBPipeline1ppField11: TppField;
    ppDBPipeline1ppField12: TppField;
    pptekrapor: TppReport;
    ppTitleBand1: TppTitleBand;
    ppLabel1: TppLabel;
    ppLabel8: TppLabel;
    ppLabel9: TppLabel;
    ppHeaderBand1: TppHeaderBand;
    ppLine1: TppLine;
    ppLabel2: TppLabel;
    ppLabel3: TppLabel;
    ppLabel4: TppLabel;
    ppLabel5: TppLabel;
    ppLabel6: TppLabel;
    ppLabel7: TppLabel;
    ppDetailBand1: TppDetailBand;
    ppDBText1: TppDBText;
    ppDBText2: TppDBText;
    ppDBText3: TppDBText;
    ppDBText4: TppDBText;
    ppDBText5: TppDBText;
    ppFooterBand1: TppFooterBand;
    ppLine2: TppLine;
    ppSystemVariable1: TppSystemVariable;
    ppSystemVariable2: TppSystemVariable;
    Panel2: TPanel;
    pKapat: TPanel;
    BitBtn5: TBitBtn;
    BitBtn1: TBitBtn;
    bYazdir: TBitBtn;
    wwDBGrid1: TwwDBGrid;
    Panel1: TPanel;
    Label14: TLabel;
    Label15: TLabel;
    Bevel1: TBevel;
    Label2: TLabel;
    durumu: TwwDBComboBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    tipi: TwwDBLookupCombo;
    FirmaAdi: TwwDBEdit;
    basTar: TwwDBDateTimePicker;
    BitTar: TwwDBDateTimePicker;
    CheckBox4: TCheckBox;
    cikis: TwwDBComboBox;
    CikDepo: TwwDBComboBox;
    wwDBEdit1: TwwDBEdit;
    CheckBox3: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    TekniktipiS: TDataSource;
    Tekniktipi: TADOQuery;
    ppLabel11: TppLabel;
    ppDBText6: TppDBText;
    ppLabel12: TppLabel;
    ppDBText7: TppDBText;
    carituruc: TCheckBox;
    carituruk: TwwDBLookupCombo;
    ExtraOptions1: TExtraOptions;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
    procedure cariara(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure wwDBGrid1DblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CheckBox4Click(Sender: TObject);
    procedure bYazdirClick(Sender: TObject);
    procedure CikDepoCloseUp(Sender: TwwDBComboBox; Select: Boolean);
    procedure CheckBox5Click(Sender: TObject);
    procedure stokKodubul(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure CheckBox6Click(Sender: TObject);
    procedure wwDBGrid1KeyPress(Sender: TObject; var Key: Char);
    procedure wwDBGrid1CalcTitleAttributes(Sender: TObject;
      AFieldName: string; AFont: TFont; ABrush: TBrush;
      var ATitleAlignment: TAlignment);
    procedure wwDBGrid1CalcTitleImage(Sender: TObject; Field: TField;
      var TitleImageAttributes: TwwTitleImageAttributes);
    procedure wwDBGrid1TitleButtonClick(Sender: TObject;
      AFieldName: string);
    procedure FormShow(Sender: TObject);
    procedure cariturucClick(Sender: TObject);
  private
    { Private declarations }
  public
    sira: string[25];
    bk: string[10];

    { Public declarations }
  end;

var
  Ftdurum: TFtdurum;
  stokkod, carikod: integer;

implementation

uses data, udmTeknik, cariaral, ucariara2, stokaral, Unit2;

{$R *.DFM}

procedure TFtdurum.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  tekniktipi.close;

  action := cafree;
  Ftdurum := nil;

end;

procedure TFtdurum.BitBtn1Click(Sender: TObject);
begin
  if (trim(FirmaAdi.text) = '') then
  begin
    Firmaadi.Text := '';
  end;

  dmTeknik.qTekLst.Close;
  dmTeknik.qTekLst.SQL.Clear;
  dmTeknik.qTekLst.SQL.Add('Select teknik.tarih,OnarimTar=(select max(onarim.bittarih)from onarim where (teknik.formno=onarim.formno)), teslimtarihi, FormNo=teknik.formno, SeriNo=teknik.Serino, Durumu=teknik.Durumu, StokKodu=stok.stokkodu, Cinsi=stok.cinsi, ');
  dmTeknik.qTekLst.SQL.Add('CariKodu=(select carikodu from cari where (teknik.CariKodGir=cari.carikod)),');
  dmTeknik.qTekLst.SQL.Add('FirmaAdi=(select FirmaAdi from cari where (teknik.CariKodGir=cari.carikod)),');
  dmTeknik.qTekLst.SQL.Add('carituru=(select tipi from cari where (teknik.CariKodGir=cari.carikod))');
  dmTeknik.qTekLst.SQL.Add('from teknik, stok');
  dmTeknik.qTekLst.SQL.Add('where  (teknik.stokkod=stok.stokkod)');
  if durumu.Value = 'G' then
    dmTeknik.qTekLst.SQL.Add('and (teknik.teslimtarihi>:bastar) and (teknik.teslimtarihi<:bittar)')
  else
    if durumu.Value = 'T' then
      dmTeknik.qTekLst.SQL.Add('and ((select max(onarim.bittarih)from onarim where (teknik.formno=onarim.formno))>:bastar) and ((select max(onarim.bittarih)from onarim where (teknik.formno=onarim.formno))<:bittar)')
    else
      dmTeknik.qTekLst.SQL.Add('and (teknik.tarih>:bastar) and (teknik.tarih<:bittar)');
  if (carikod > 0) and CheckBox3.Checked then
    dmTeknik.qTekLst.SQL.Add('and (CarikodGir=' + inttostr(carikod) + ')');
  if CheckBox1.Checked then
    dmTeknik.qTekLst.SQL.Add('and (Durumu=''' + durumu.value + ''')');
  if CheckBox2.Checked then
    dmTeknik.qTekLst.SQL.Add('and (Tipi=''' + Tipi.LookupValue + ''')');
  if CheckBox4.Checked then
    dmTeknik.qTekLst.SQL.Add('and (teknik.cikisi=''' + cikis.Value + ''')');
  if CheckBox5.Checked then
    dmTeknik.qTekLst.SQL.Add('and (DepokodCik=''' + CikDepo.value + ''')');

  if carituruc.Checked then
    dmTeknik.qTekLst.SQL.Add('and ((select tipi from cari where (teknik.CariKodGir=cari.carikod))=''' + carituruk.lookupvalue + ''')');

  if (stokkod > 0) and CheckBox6.Checked then
    dmTeknik.qTekLst.SQL.Add('and (teknik.stokkod=' + inttostr(stokkod) + ')');

  dmTeknik.qTekLst.SQL.Add('order by teknik.formno');

  dmTeknik.qTekLst.Parameters.ParamByName('BasTar').datatype := ftDate;
  dmTeknik.qTekLst.Parameters.ParamByName('BitTar').datatype := ftDate;
  dmTeknik.qTekLst.Parameters.ParamByName('BasTar').value := BasTar.date;
  dmTeknik.qTekLst.Parameters.ParamByName('BitTar').value := BitTar.date + 1;

  // memo1.clear;
   //memo1.Lines.AddStrings(dmTeknik.qTekLst.SQL);
  dmTeknik.qTekLst.Open;
  sira := 'tarih';
  bk := ' ASC';
  dmTeknik.qTekLst.Sort := sira + bk;

  Label2.Caption := '(' + inttostr(dmTeknik.qTekLst.recordcount) + ')';
  wwDBGrid1.SetFocus;
end;

procedure TFtdurum.BitBtn5Click(Sender: TObject);
begin
  close;
end;

procedure TFtdurum.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
  begin
    durumu.Enabled := true;
    durumu.ItemIndex := 0;
    durumu.setfocus;
  end
  else
  begin
    durumu.ItemIndex := -1;
    durumu.Enabled := False;
  end;
end;

procedure TFtdurum.CheckBox2Click(Sender: TObject);
begin
  if CheckBox2.Checked then
  begin
    Tipi.Enabled := true;
    Tipi.SetFocus;
  end
  else
  begin
    tipi.value := '';
    Tipi.Enabled := False;
  end;
end;

procedure TFtdurum.CheckBox3Click(Sender: TObject);
begin
  if CheckBox3.Checked then
  begin
    FirmaAdi.Enabled := True;
    FirmaAdi.text := '';
    carikod := 0;
    if CheckBox5.Checked then
      CheckBox5.Checked := false;
    firmaadi.SetFocus;
  end
  else
  begin
    FirmaAdi.Enabled := False;
    firmaadi.Text := '';
  end;
end;

procedure TFtdurum.cariara(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  t: integer;
begin
  t := TwwDBEdit(sender).Tag;
  if (key = 13) and ((sender as TwwDBEdit).text <> '') then
  begin
    dm.Cariara.Close;
    dm.Cariara.SQL.Clear;
    dm.Cariara.SQL.Add('select distinct carikod,KARGOKOD, carikodu, istihbarat,Tekyetkili, CTipi=(select adi from dbo.carituru where id=Tipi), FirmaAdi, Soyadi, ilce, Sehir,telefon3,KARGOADI=(select top 1 Ad from tbl_KargoTanim where ID=KARGOKOD) from Cari');

    dm.Cariara.SQL.Add('where (CariKod>0)');
    if t = 1 then
      dm.Cariara.SQL.Add('and (carikodu like ''' + TEdit(sender).text +
        '%'' )');
    if t = 2 then
      dm.Cariara.SQL.Add('and (FirmaAdi like ''' + TEdit(sender).text +
        '%'' )');
    dm.Cariara.Open;
    if (dm.CariAra.RecordCount = 1) then
    begin
      cariKod := dm.cariara.FieldByName('carikod').value;
      //     carikodu.text:=dm.cariara.FieldByName('CariKodu').asstring;
      FirmaAdi.text := dm.cariara.FieldByName('firmaadi').asstring;
    end
    else
    begin
      if (dm.cariAra.RecordCount > 1) then
      begin
        if not assigned(fCariaraL) then
          fCariAraL := tfCariAral.create(self);
        if t = 1 then
        begin
          fCariAraL.siras := 'carikodu';
          fCariAraL.aranan := TEdit(sender).text;
        end;
        if t = 2 then
        begin
          fCariAraL.siras := 'FirmaAdi';
          fCariAraL.aranan := TEdit(sender).text;
        end;

        if fCariAraL.showmodal = mrOk then
        begin
          cariKod := dm.cariara.FieldByName('carikod').value;
          //     carikodu.text:=dm.cariara.FieldByName('CariKodu').asstring;
          FirmaAdi.text := dm.cariara.FieldByName('firmaadi').asstring;
        end
        else
        begin
          fCariAra2 := tfCariAra2.create(self);
          if t = 1 then
            fCariAra2.edit3.Text := TEdit(sender).text;
          if t = 2 then
            fCariAra2.edit4.Text := TEdit(sender).text;
          if fCariAra2.showmodal = mrok then
          begin
            if dm.CariAra.Active = true then
            begin
              cariKod := dm.cariara.FieldByName('carikod').value;
              //     carikodu.text:=dm.cariara.FieldByName('CariKodu').asstring;
              FirmaAdi.text := dm.cariara.FieldByName('firmaadi').asstring;
            end;
          end;
        end;
      end
      else
      begin
        fCariAra2 := tfCariAra2.create(self);
        if t = 1 then
          fCariAra2.edit3.Text := TEdit(sender).text;
        if t = 2 then
          fCariAra2.edit4.Text := TEdit(sender).text;
        fCariAra2.gelis := t;
        if fCariAra2.showmodal = mrok then
        begin
          if dm.CariAra.Active = true then
          begin
            cariKod := dm.cariara.FieldByName('carikod').value;
            //     carikodu.text:=dm.cariara.FieldByName('CariKodu').asstring;
            FirmaAdi.text := dm.cariara.FieldByName('firmaadi').asstring;
          end;
        end;
      end;
    end;
  end;

end;

procedure TFtdurum.wwDBGrid1DblClick(Sender: TObject);
begin
  if not dmTeknik.qTekLst.IsEmpty then
    modalresult := mrok;
end;

procedure TFtdurum.FormCreate(Sender: TObject);
begin
  dm.spdepo.Parameters.parambyname('@depokod').Value := 0;
  dm.spdepo.Open;
  dm.spdepo.First;
  CikDepo.Items.Clear;
  while (not dm.spdepo.Eof) do
  begin
    CikDepo.Items.Add(dm.spdepo.FieldByName('firmaadi').asstring + #9 + dm.spdepo.FieldByName('carikod').asstring);
    dm.spdepo.Next;
  end;
  CikDepo.ApplyList;

  if not dmTeknik.sptekniktipi.Active then
    dmTeknik.sptekniktipi.Open;

  dm.sppersonel.Filter := 'Departman=' + #39 + '1' + #39;
  dm.sppersonel.Filtered := true;

//  dm.carituru.open;

  //  dmTeknik.qTekLst.Open;
  carikod := 0;
end;

procedure TFtdurum.CheckBox4Click(Sender: TObject);
begin
  if CheckBox4.Checked then
  begin
    cikis.itemindex := 0;
    cikis.Enabled := true;
    cikis.SetFocus;
  end
  else
  begin
    cikis.itemindex := -1;
    cikis.Enabled := False;
  end;
end;

procedure TFtdurum.bYazdirClick(Sender: TObject);
begin
  dmTeknik.qTekLst.DisableControls;
  pplabel8.Caption := '( ' + durumu.Text + ' )';

  if CheckBox2.Checked then
  begin
    pplabel9.Caption := '( ' + tipi.Text + ' )'
  end
  else
    pplabel9.Caption := '';
  pptekrapor.print;

  dmTeknik.qTekLst.EnableControls;
end;

procedure TFtdurum.CikDepoCloseUp(Sender: TwwDBComboBox; Select: Boolean);
begin
  if select then
  begin
    FirmaAdi.Text := '';
  end;
end;

procedure TFtdurum.CheckBox5Click(Sender: TObject);
begin
  if CheckBox5.Checked then
  begin
    CikDepo.Enabled := true;
    CikDepo.ItemIndex := 0;
    CikDepo.SetFocus;
    if CheckBox3.Checked then
      CheckBox3.Checked := false;
  end
  else
  begin
    CikDepo.ItemIndex := -1;
    CikDepo.Enabled := False;
  end;
end;

procedure TFtdurum.stokKodubul(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  t: integer;
begin
  t := TEdit(sender).Tag;
  if (key = 13) and (TEdit(sender).text <> '') then
  begin
    dm.stokara.Close;
    dm.stokara.SQL.Clear;
    dm.stokara.SQL.Add(dm.StokilkSql);
    dm.stokara.SQL.Add('where (stok.stokkod>0) ');
    if t = 1 then
      dm.stokara.SQL.Add('and stokkodu like ''' + TEdit(sender).text + '%''');
    if t = 2 then
      dm.stokara.SQL.Add('and cinsi like ''' + TEdit(sender).text + '%''');
    dm.stokara.Open;

    if (dm.stokara.RecordCount = 1) then
    begin
      stokkod := dm.stokara.Fieldbyname('stokkod').asInteger;
      wwDBEdit1.text := dm.stokara.Fieldbyname('Cinsi').asString;
    end
    else
    begin
      if (dm.stokara.RecordCount > 1) then
      begin
        if not assigned(fStokaral) then
          fStokaral := tfStokaral.create(self);

        if fstokaral.showmodal = mrOk then
        begin
          stokkod := dm.stokara.Fieldbyname('stokkod').asInteger;
          wwDBEdit1.text := dm.stokara.Fieldbyname('Cinsi').asString;
        end
        else
        begin
          fstokara2 := tfstokara2.create(self);
          if t = 1 then
            fstokara2.edit3.Text := TEdit(sender).text;
          if t = 2 then
            fstokara2.edit4.Text := TEdit(sender).text;
          if fstokara2.showmodal = mrok then
          begin
            if dm.stokara.Active = true then
            begin
              stokkod := dm.stokara.Fieldbyname('stokkod').asInteger;
              wwDBEdit1.text := dm.stokara.Fieldbyname('Cinsi').asString;
            end;
          end;
        end;
      end
      else
      begin
        fstokara2 := tfstokara2.create(self);
        if t = 1 then
          fstokara2.edit3.Text := TEdit(sender).text;
        if t = 2 then
          fstokara2.edit4.Text := TEdit(sender).text;
        fstokara2.gelis := t;
        if fstokara2.showmodal = mrok then
        begin
          if dm.stokara.Active = true then
          begin
            stokkod := dm.stokara.Fieldbyname('stokkod').asInteger;
            wwDBEdit1.text := dm.stokara.Fieldbyname('Cinsi').asString;
          end;
        end;
      end;
    end;
  end;

end;

procedure TFtdurum.CheckBox6Click(Sender: TObject);
begin
  if CheckBox6.Checked then
  begin
    wwDBEdit1.Enabled := true;
    wwdbedit1.setfocus;
  end
  else
  begin
    wwDBEdit1.Enabled := False;
    wwDBEdit1.Text := '';
  end;
end;

procedure TFtdurum.wwDBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
  begin
    key := #0;
    ModalResult := mrOk;
  end;
end;

procedure TFtdurum.wwDBGrid1CalcTitleAttributes(Sender: TObject;
  AFieldName: string; AFont: TFont; ABrush: TBrush;
  var ATitleAlignment: TAlignment);
begin
  if sira = AFieldName then
    AFont.Style := [fsBold]
  else
    AFont.Style := [];
end;

procedure TFtdurum.wwDBGrid1CalcTitleImage(Sender: TObject; Field: TField;
  var TitleImageAttributes: TwwTitleImageAttributes);
begin
  if field.FieldName = sira then
  begin
    if bk = ' ASC' then
      TitleImageAttributes.imageIndex := 0
    else
      TitleImageAttributes.imageIndex := 1;
  end
  else
    TitleImageAttributes.imageIndex := -1;
end;

procedure TFtdurum.wwDBGrid1TitleButtonClick(Sender: TObject;
  AFieldName: string);
begin
  if AFieldName = sira then
  begin
    if bk = ' ASC' then
      bk := ' DESC'
    else
      bk := ' ASC';
  end
  else
    bk := ' ASC';
  sira := AFieldName;
  dmTeknik.qTekLst.Sort := sira + bk;
  wwDBGrid1.RedrawGrid;
end;

procedure TFtdurum.FormShow(Sender: TObject);
begin
  tekniktipi.open;

end;

procedure TFtdurum.cariturucClick(Sender: TObject);
begin
  if carituruc.Checked then
  begin
    carituruk.Enabled := true;
    carituruk.SetFocus;
  end
  else
  begin
    carituruk.value := '';
    carituruk.Enabled := False;
  end;

end;

end.

